<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row v-show="showSearch">
				<div class="flex justify-between w-full mb8">
					<el-space>
						<el-popover trigger="click" :width="430" placement="bottom-start">
							<el-form ref="queryRef" :model="state.queryForm" label-width="70px" @keyup.enter="getDataList">
								<el-row>
									<el-col :span="24" class="mb20">
										<el-form-item label="单位" prop="deptId">
											<el-tree-select
												v-model="state.queryForm.deptId"
												:data="parentData"
												:props="{ value: 'id', label: 'name', children: 'children', disabled: 'isLock' }"
												clearable
												filterable
												check-strictly
												multiple
												tag-type="success"
												:render-after-expand="false"
												placeholder="请选择单位"
												:teleported="false"
											/>
										</el-form-item>
									</el-col>
									<el-col :span="24" class="mb20">
										<el-form-item label="设备IMEI" prop="imei">
											<el-input placeholder="请输入设备IMEI" v-model="state.queryForm.imei" />
										</el-form-item>
									</el-col>
									<el-col :span="24">
										<el-form-item label="日期" prop="createTime">
											<el-date-picker
												:end-placeholder="$t('syslog.inputEndPlaceholderTip')"
												:start-placeholder="$t('syslog.inputStartPlaceholderTip')"
												range-separator="To"
												type="datetimerange"
												v-model="state.queryForm.createTime"
												value-format="YYYY-MM-DD HH:mm:ss"
												:teleported="false"
											/>
										</el-form-item>
									</el-col>
								</el-row>
							</el-form>
							<template #reference>
								<el-button link type="primary"
									>高级搜索<el-icon><ArrowDown /></el-icon>
								</el-button>
							</template>
						</el-popover>
						<el-input placeholder="请输入出库单号" v-model="state.queryForm.outInventoryNumber" />
						<el-button icon="Search" type="primary" @click="getDataList">{{ $t('common.queryBtn') }}</el-button>
						<el-button icon="Refresh" @click="resetQuery"></el-button>
					</el-space>
				</div>
			</el-row>
			<!-- <el-row>
				<div class="mb8" style="width: 100%">
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'platform_basicInventoryHistory_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row> -->
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="outInventoryNumber" label="出库单号" show-overflow-tooltip />
				<el-table-column prop="customerDeptName" label="客户单位" show-overflow-tooltip />
				<el-table-column prop="deviceSum" label="设备数" show-overflow-tooltip />
				<el-table-column prop="outInventoryType" label="出库模式" show-overflow-tooltip>
					<template #default> 默认出库 </template>
				</el-table-column>
				<el-table-column prop="createTime" label="出库时间" show-overflow-tooltip />
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<!-- <el-button
							icon="edit-pen"
							text
							type="primary"
							v-auth="'platform_basicInventoryHistory_edit'"
							@click="formDialogRef.openDialog(scope.row.id)"
							>编辑</el-button
						>
						<el-button icon="delete" text type="primary" v-auth="'platform_basicInventoryHistory_del'" @click="handleDelete([scope.row.id])"
							>删除</el-button
						> -->
						<el-button size="small" type="primary" @click="detailDialogRef.openDialog(scope.row.outInventoryNumber)">查看</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
		<!-- 查看详情 -->
		<detail-dialog ref="detailDialogRef" />
	</div>
</template>

<script setup lang="ts" name="basicInventoryHistory">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList } from '/@/api/platform/basicInventoryHistory';
import { deptTree } from '/@/api/admin/dept';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const DetailDialog = defineAsyncComponent(() => import('./components/detail.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
const detailDialogRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
const parentData = ref([]);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	state.queryForm.outInventoryNumber = null;
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/platform/basicInventoryHistory/export', Object.assign(state.queryForm, { ids: selectObjs }), 'basicInventoryHistory.xlsx');
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};
const getDeptData = async () => {
	deptTree().then((res) => {
		parentData.value = res.data;
	});
};
getDeptData();
</script>
